<template>
	<view>
		<view class="uploadBlock">
			<view class="title">{{pageTitle}}</view>

			<u-upload :action="action" :custom-btn="true" :max-count="maxCount" :show-upload-list="true" ref="uUpload"
				name="uploadFiles" @on-uploaded="uploadOver">
				<view slot="addBtn" class="uploadBox" hover-class="slot-btn__hover" hover-stay-time="150">
					<img src="https://supply.zhongyimotor.com/upload/applet/upload@3x.png" alt="">
				</view>
			</u-upload>
		</view>
		<u-button type="primary" @click="submit" :disabled="isDisable">确认上传</u-button>
		<u-toast ref="uToast" />
		<u-no-network></u-no-network>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				type: "",
				pageTitle: "",
				action: "",
				isDisable: true,
				completedList: [],
				maxCount: 99
			}
		},
		onLoad(option) {
			this.uploadType(option)
		},
		onShow() {
			this.action = this.$config.uploadFilesUrl
		},
		methods: {
			submit() {
				let that = this
				that.isDisable = true
				that.$refs.uUpload.lists.forEach((item) => {
					if (item.progress == 100) {
						that.completedList.push(item.response.result[0].url)
					}
				})
				switch (that.type) {
					case "contract":
						var params = {
							files: that.completedList,
							orderId: this.orderId
						}
						that.$api.signContract(params).then((res) => {
							if (res.resultCode == 0) {
								that.uploadSuccess()
							}
						})
						break;
					case "bond":
						var params = {
							marginPath: that.completedList[0],
							orderId: this.orderId
						}
						that.$api.uploadMargin(params).then((res) => {
							if (res.resultCode == 0) {
								that.uploadSuccess()
							}
						})
						break;
					case "loan":
						var params = {
							repaymentPath: that.completedList[0],
							orderId: this.orderId
						}
						that.$api.uploadRepayment(params).then((res) => {
							if (res.resultCode == 0) {
								that.uploadSuccess()
							}
						})
						break;
				}
			},
			uploadSuccess() {
				let that = this
				uni.showToast({
					title: '上传成功',
					icon: "success",
					duration: 2000,
					success: () => {
						setTimeout(() => {
							that.isDisable = false
							let pages = getCurrentPages();
							let prePage = pages[pages.length - 2]
							uni.navigateBack({
								delta: 1,
								success(event) {
									prePage.$vm.reload()
								}
							});
						}, 2000)
					}
				})
			},
			uploadOver(lists, name) {
				this.isDisable = false
			},
			uploadType(option) {
				this.type = option.type
				switch (this.type) {
					case "contract":
						this.pageTitle = "请上传签署后的合同文件(可多个合同一并上传)"
						this.orderId = option.orderId
						break;
					case "bond":
						this.pageTitle = "请上传保证金支付凭证"
						this.maxCount = 1
						this.orderId = option.orderId
						break;
					case "loan":
						this.pageTitle = "请上传还款凭证"
						this.maxCount = 1
						this.orderId = option.orderId
						break;
					default:
						this.pageTitle = "请上传文件"
						break;
				}
			}
		}
	};
</script>

<style scoped lang="scss">
	.uploadBlock {
		padding: 30rpx;

		.title {
			font-size: 32rpx;
			color: #202020;
		}
	}

	.uploadBox {
		width: 690rpx;
		height: 460rpx;
		border-radius: 6rpx;
		margin: 30rpx auto;
		background: #F4F8FF
	}

	/deep/button {
		width: 630rpx;
		height: 94rpx;
	}

	.pre-box {
		display: flex;
		flex-wrap: wrap;

		.pre-item {
			width: 230rpx;
			height: 230rpx;
			padding: 10rpx;

			.pre-item-image {
				width: 100%;
				height: 100%;
				line-height: 0;
			}
		}
	}
</style>